package vn.edu.saigontech.ncms.web.bookstore;

import java.util.Collection;
import java.util.List;

import vn.edu.saigontech.ncms.model.bookstore.Item;
import vn.edu.saigontech.ncms.model.bookstore.ItemTitle;
import vn.edu.saigontech.ncms.model.bookstore.ItemType;
import vn.edu.saigontech.ncms.model.enumeration.Currency;
import vn.edu.saigontech.ncms.model.program.Program;
import vn.edu.saigontech.ncms.model.system.Money;
import vn.edu.saigontech.ncms.service.ItemService;
import vn.edu.saigontech.ncms.service.ItemTitleService;
import vn.edu.saigontech.ncms.service.ItemTypeService;
import vn.edu.saigontech.ncms.service.ProgramService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class ItemAction extends BaseAction {
	private Long id;

	private Long titleId;
	private Long typeId;
	private String itemId;
	private double price;
	private boolean active;
	private String comment;
	
	private Long userId;
	
	private Long programId;
	private Program program;
	private Collection<Program> programs;

	private ItemType type;
	private Collection<ItemType> types;
	
	private Item item;
	private List<Item> items;

	/**
	 * show add an item form
	 * 
	 * @return
	 */
	public String showAdd() {
		this.program = ProgramService.INSTANCE.findById(this.programId);
		this.item = ItemService.INSTANCE.findById(this.id);
		this.programs = ProgramService.INSTANCE.findAll();
		this.types = ItemTypeService.INSTANCE.findAll();
		return SUCCESS;
	}

	/**
	 * add an item into system
	 * 
	 * @return
	 */
	public String add() {
		Item item = new Item();
		ItemTitle title = ItemTitleService.INSTANCE.findById(this.titleId);
		item.setItemTitle(title);

		ItemType type = ItemTypeService.INSTANCE.findById(this.typeId);
		item.setItemType(type);

		item.setItemId(this.itemId);
		item.setPrice(new Money(this.price, Currency.VND));
		item.setActive(true);
		item.setComment(this.comment);
		
		item = ItemService.INSTANCE.persist(item);
		
		// to redirect to listByItem
		this.id = item.getId();
		
		return SUCCESS;
	}

	/**
	 * list items by program and item-type
	 * 
	 * @return
	 */
	public String list() {
		this.program = ProgramService.INSTANCE.findById(this.programId);
		this.type = ItemTypeService.INSTANCE.findById(this.typeId);
		if(Checker.isNull(program)) {
			this.program = ProgramService.INSTANCE.getFirst();
		}
		this.types = ItemTypeService.INSTANCE.findAll();
		this.programs = ProgramService.INSTANCE.findAll();
		
		this.items = ItemService.INSTANCE.filter(program, type);
		return SUCCESS;
	}
	
	/**
	 * list items by program and item-type depends on item just added
	 * 
	 * @return
	 */
	public String listByItem() {
		this.item = ItemService.INSTANCE.findById(this.id);
		
		this.types = ItemTypeService.INSTANCE.findAll();
		this.programs = ProgramService.INSTANCE.findAll();
		
		this.program = this.item.getItemTitle().getProgram();
		this.items = ItemService.INSTANCE.filter(program, null);
		
		// re-direct
		this.programId = this.program.getId();
		return SUCCESS;
	}
	
	public String showEdit() {
		item = ItemService.INSTANCE.findById(this.id);
		this.program = item.getItemTitle().getProgram();
		this.programs = ProgramService.INSTANCE.findAll();
		this.types = ItemTypeService.INSTANCE.findAll();
		return SUCCESS;
	}
	
	public String edit() {
		item = ItemService.INSTANCE.findById(this.id);
		
		ItemTitle title = ItemTitleService.INSTANCE.findById(this.titleId);
		item.setItemTitle(title);
		
		ItemType type = ItemTypeService.INSTANCE.findById(this.typeId);
		item.setItemType(type);
		
		item.setItemId(this.itemId);
		item.setPrice(new Money(this.price, Currency.VND));
		item.setActive(this.active);
		item.setComment(this.comment);
		
		// to redirect to listByItem
		this.id = item.getId();
		
		return SUCCESS;
	}
	
	public String delete() {
		Item item = ItemService.INSTANCE.findById(this.id);
		String name = item.getItemTitle().getTitle();
		String type = item.getItemType().getName();
		if (ItemService.INSTANCE.deleteById(this.id)) {
			this.setKeyMessage("confirm.deleteSuccessfully", "bookstore.item");
			this.addLoggingDetail(BaseAction.DELETE, "logging.item.delete", name, type);
		} else 
			this.setKeyMessage("confirm.deleteUnsuccessfully", "bookstore.item");
		return SUCCESS;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getTitleId() {
		return titleId;
	}

	public void setTitleId(Long titleId) {
		this.titleId = titleId;
	}

	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public Program getProgram() {
		return program;
	}

	public void setProgram(Program program) {
		this.program = program;
	}

	public Collection<Program> getPrograms() {
		return programs;
	}

	public void setPrograms(Collection<Program> programs) {
		this.programs = programs;
	}

	public Long getProgramId() {
		return programId;
	}

	public void setProgramId(Long programId) {
		this.programId = programId;
	}

	public Collection<ItemType> getTypes() {
		return types;
	}

	public void setTypes(Collection<ItemType> types) {
		this.types = types;
	}

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public ItemType getType() {
		return type;
	}

	public void setType(ItemType type) {
		this.type = type;
	}

	public List<Item> getItems() {
		return items;
	}

	public void setItems(List<Item> items) {
		this.items = items;
	}

	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}
}
